<?php
namespace app\account\controller;

use tp5_lib\account\logic\AccountLogic;
use tp5_lib\user\model\UserModel;
use tp5_lib\common\controller\TestBaseController;
use tp5_lib\account\logic\RechargeLogic;

class TestController extends TestBaseController
{
    public function index()
    {
        return $this->fetch();
    }
    /**
     * 查询余额
     */
    public function doQuery()
    {
        $uid=input("uid");
        $model=new UserModel();
        if(!$user=$model->db()->where(["id"=>$uid])->find()){
            $this->error("用户不存在");
        }
        $this->success("余额:" . $user["money"]);
    }
    /**
     * 修改余额
     */
    public function doChange()
    {
        $logic=new AccountLogic();
        if(!$logic->change("0",input("uid"),input("amount"),"测试")){
            $this->error($logic->getError());
        }
        $this->success("修改成功");
    }
    
    public function doWithdraw()
    {
        if(!$id=input("id")){
            $this->error("提现ID不能为空");
        }
        $logic=new AccountLogic();
        if(!$logic->doWithdraw($id,"0")){
            $this->error($logic->getError());
        }
        $this->success();
    }
    public function cancelWithdraw()
    {
        if(!$id=input("id")){
            $this->error("提现ID不能为空");
        }
        $logic=new AccountLogic();
        if(!$logic->cancelWithdraw($id,"0")){
            $this->error($logic->getError());
        }
        $this->success();
    }

    /**
     * 测试创建充值订单
     */
    public function recharge()
    {
        $data=input();
        $logic=new RechargeLogic();
        $id=$logic->create($data["uid"], $data["amount"]);
        $account=new AccountLogic();
        return $account->precreate($data["way"],"payment",$id,$data["amount"],"支付");
    }
    /**
     * 测试充值成功
     */
    public function doRecharge()
    {
        if(!$id=input("id")){
            $this->error("充值ID不能为空");
        }
        $logic=new RechargeLogic();
        if(!$logic->paySuccess($id)){
            $this->error($logic->getError());
        }
        $this->success("充值成功");
    }

    /**
     * 测试支付成功
     * 自动调用相应的逻辑处理订单
     */
    public function doSuccess()
    {
        if(!$no=input("no")){
            $this->error("交易编号不能为空");
        }
        $logic=new AccountLogic();
        if(!$logic->success($no)){
            $this->error($logic->getError());
        }
        $this->success("交易成功");
    }
    /**
     * 支付关闭
     */
    public function onClose()
    {

    }
}

